﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PTCTTCNBK.Models;

namespace TTCNBK_2014.Controllers
{
    public class FunctionController : Controller
    {
        public string Key1 = "FunctionOrderController";
        public string Key2 = "FunctionImportController";
        public string Key3 = "FunctionCheckInController";

        public ActionResult OpClickSupport(int id)
        {
            Session["Op"] = id;

            return RedirectToAction("Order");
        }

        //------------------------------------Order------------------------------------------------------------------------

        public ActionResult Order()
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key1] == null || (bool)Session[Key1] != true)
                return RedirectToAction("Login", "Login");
            
            var dataContext = new PetroDataContext();
            BillAndProduct itemResult = new BillAndProduct();
            itemResult.Bill = dataContext.PtcTbl_Bills.Where(p => p.Type == false && p.IsDeleted == false && p.UserID == (int)Session["UserID"]).OrderByDescending(p => p.ID).ToList();
            itemResult.Product = dataContext.PtcTbl_Products.ToList();

            ViewBag.Totalrecords = itemResult.Bill.Count();
            
            if (Session["Op"] == null)
            {
                ViewBag.RecordsPerPage = 5;
            }
            else
            {
                ViewBag.RecordsPerPage = (int)Session["Op"];
            }

            return View(itemResult);
        }

        public ActionResult AddOrder(DateTime deliveryDate, int productID, double volume, double temp, string deliveryAddress, int transportation)
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key1] == null || (bool)Session[Key1] != true)
                return RedirectToAction("Login", "Login");

            // Get price product
            var dataContext = new PetroDataContext();
            PtcTbl_Product product = dataContext.PtcTbl_Products.FirstOrDefault(p => p.ID == productID);
            double price = 1;

            if (product != null)
            {
                price = product.Price;
            }

            PtcTbl_Bill itemNew = new PtcTbl_Bill();

            itemNew.PlaceDelivery = deliveryAddress;
            itemNew.ProductID = productID;
            itemNew.DateDelivery = deliveryDate;
            itemNew.Amount = volume;
            itemNew.Temperature = temp;
            itemNew.Type = false;
            itemNew.IsDeleted = false;
            itemNew.VehicleID = transportation;
            itemNew.Price = volume * price;
            itemNew.Date = DateTime.Now;
            itemNew.UserID = (int)Session["UserID"];
            itemNew.Status = 0;

            dataContext.PtcTbl_Bills.InsertOnSubmit(itemNew);
            dataContext.SubmitChanges();

            return RedirectToAction("Order");
        }

        public ActionResult DeleteOrder()
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key1] == null || (bool)Session[Key1] != true)
                return RedirectToAction("Login", "Login");

            List<int> lstId = (List<int>)Session["ListItemDelete"];

            if (lstId != null && lstId.Count > 0)
            {
                PetroDataContext dataContext = new PetroDataContext();

                PtcTbl_Bill billResult;
                foreach (int id in lstId)
                {
                    billResult = dataContext.PtcTbl_Bills.FirstOrDefault(p=>p.ID == id);

                    if (billResult != null)
                    {
                        billResult.IsDeleted = true;
                    }
                }

                lstId = new List<int>();
                Session["ListItemDelete"] = lstId;

                dataContext.SubmitChanges();
            }

            return RedirectToAction("Order");
        }

        public ActionResult EditOrder(DateTime deliveryDate2, int productID2, double volume2, double temp2, string deliveryAddress2, int transportation2)
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key1] == null || (bool)Session[Key1] != true)
                return RedirectToAction("Login", "Login");

            int BillID = (int)Session["BillID"];

            var dataContext = new PetroDataContext();

            PtcTbl_Bill itemEdit = dataContext.PtcTbl_Bills.FirstOrDefault(p => p.ID == BillID);
            
            PtcTbl_Product product = dataContext.PtcTbl_Products.FirstOrDefault(p => p.ID == productID2);
            double price = 1;

            if (product != null)
            {
                price = product.Price;
            }

            if (itemEdit != null)
            {
                itemEdit.DateDelivery = deliveryDate2;
                itemEdit.ProductID = productID2;
                itemEdit.Amount = volume2;
                itemEdit.Temperature = temp2;
                itemEdit.PlaceDelivery = deliveryAddress2;
                itemEdit.VehicleID = transportation2;
                itemEdit.Date = DateTime.Now;
                itemEdit.Price = price*volume2;            
            }
            dataContext.SubmitChanges();
            return RedirectToAction("Order");
        }

        public ActionResult EditOrders(int id)
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key1] == null || (bool)Session[Key1] != true)
                return RedirectToAction("Login", "Login");

            Session["BillID"] = id;
            var dataContext = new PetroDataContext();

            PtcTbl_Bill itemEdit = dataContext.PtcTbl_Bills.FirstOrDefault(p => p.ID == id);

            ViewBag.Product = dataContext.PtcTbl_Products.ToList();

            return View(itemEdit);
        }

        public ActionResult SearchOrder(string deliveryFromDate3, string deliveryToDate3, int productID3, int transportation3, string deliveryAddress3)
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key1] == null || (bool)Session[Key1] != true)
                return RedirectToAction("Login", "Login");

            var dataContext = new PetroDataContext();
            BillAndProduct res = new BillAndProduct();
            DateTime fromDate;
            DateTime toDate;

            if (deliveryFromDate3 == "")
            {
                fromDate = DateTime.ParseExact("1981-01-01", "yyyy-MM-dd", null);
            }
            else
            {
                fromDate = DateTime.ParseExact(deliveryFromDate3, "yyyy-MM-dd", null);
            }

            if (deliveryToDate3 == "")
            {
                toDate = DateTime.ParseExact("2100-12-31", "yyyy-MM-dd", null);
            }
            else
            {
                toDate = DateTime.ParseExact(deliveryToDate3, "yyyy-MM-dd", null);
            }

            if (productID3 != null && productID3 != 0)
            {
                if (transportation3 != null && transportation3 != 0)
                {
                    res.Bill = dataContext.PtcTbl_Bills.Where(p => p.ProductID == productID3 && p.UserID == (int)Session["UserID"] && p.Type == false && p.IsDeleted == false && p.VehicleID == transportation3 && p.DateDelivery >= fromDate && p.DateDelivery <= toDate && p.PlaceDelivery.Contains(deliveryAddress3)).ToList();
                }
                else
                {
                    res.Bill = dataContext.PtcTbl_Bills.Where(p => p.ProductID == productID3 && p.UserID == (int)Session["UserID"] && p.Type == false && p.IsDeleted == false && p.DateDelivery >= fromDate && p.DateDelivery <= toDate && p.PlaceDelivery.Contains(deliveryAddress3)).ToList();
                }
            }
            else
            {
                if (transportation3 != null && transportation3 != 0)
                {
                    res.Bill = dataContext.PtcTbl_Bills.Where(p => p.UserID == (int)Session["UserID"] && p.Type == false && p.IsDeleted == false && p.VehicleID == transportation3 && p.DateDelivery >= fromDate && p.DateDelivery <= toDate && p.PlaceDelivery.Contains(deliveryAddress3)).ToList();
                }
                else
                {
                    res.Bill = dataContext.PtcTbl_Bills.Where(p => p.UserID == (int)Session["UserID"] && p.Type == false && p.IsDeleted == false && p.DateDelivery >= fromDate && p.DateDelivery <= toDate && p.PlaceDelivery.Contains(deliveryAddress3)).ToList();
                }
            }
                        
            res.Product = dataContext.PtcTbl_Products.ToList();

            ViewBag.Totalrecords = res.Bill.Count();

            return PartialView("Order", res);
        }

        //------------------------------------Import------------------------------------------------------------------------

        public ActionResult Import()
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key2] == null || (bool)Session[Key2] != true)
                return RedirectToAction("Login", "Login");          

            var dataContext = new PetroDataContext();
            BillAndProduct itemResult = new BillAndProduct();
            itemResult.Bill = dataContext.PtcTbl_Bills.Where(p => p.Type == true && p.IsDeleted == false && p.UserID == (int)Session["UserID"]).OrderByDescending(p => p.ID).ToList();
            itemResult.Product = dataContext.PtcTbl_Products.ToList();

            ViewBag.Totalrecords = itemResult.Bill.Count();


            if (Session["Op"] == null)
            {
                ViewBag.RecordsPerPage = 5;
            }
            else
            {
                ViewBag.RecordsPerPage = (int)Session["Op"];
            }

            return View(itemResult);
        }

        public ActionResult AddImport(DateTime deliveryDate, int productID, double volume, double temp, string deliveryAddress, int transportation)
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key2] == null || (bool)Session[Key2] != true)
                return RedirectToAction("Login", "Login");

            // Get price product
            var dataContext = new PetroDataContext();
            PtcTbl_Product product = dataContext.PtcTbl_Products.FirstOrDefault(p => p.ID == productID);
            double price = 1;

            if (product != null)
            {
                price = product.Price;
            }

            PtcTbl_Bill itemNew = new PtcTbl_Bill();

            itemNew.PlaceDelivery = deliveryAddress;
            itemNew.ProductID = productID;
            itemNew.DateDelivery = deliveryDate;
            itemNew.Amount = volume;
            itemNew.Temperature = temp;
            itemNew.Type = true;
            itemNew.IsDeleted = false;
            itemNew.VehicleID = transportation;
            itemNew.Price = volume * price;
            itemNew.Date = DateTime.Now;
            itemNew.UserID = (int)Session["UserID"];
            itemNew.Status = 0;

            dataContext.PtcTbl_Bills.InsertOnSubmit(itemNew);
            dataContext.SubmitChanges();

            return RedirectToAction("Import");
        }

        public ActionResult DeleteImport()
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key2] == null || (bool)Session[Key2] != true)
                return RedirectToAction("Login", "Login");

            List<int> lstId = (List<int>)Session["ListItemDelete"];

            if (lstId != null && lstId.Count > 0)
            {
                PetroDataContext dataContext = new PetroDataContext();

                PtcTbl_Bill billResult;
                foreach (int id in lstId)
                {
                    billResult = dataContext.PtcTbl_Bills.FirstOrDefault(p => p.ID == id);

                    if (billResult != null)
                    {
                        billResult.IsDeleted = true;
                    }
                }

                lstId = new List<int>();
                Session["ListItemDelete"] = lstId;

                dataContext.SubmitChanges();
            }

            return RedirectToAction("Import");
        }

        public ActionResult SearchImport(string deliveryFromDate3, string deliveryToDate3, int productID3, int transportation3, string deliveryAddress3)
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key2] == null || (bool)Session[Key2] != true)
                return RedirectToAction("Login", "Login");

            var dataContext = new PetroDataContext();
            BillAndProduct res = new BillAndProduct();
            DateTime fromDate;
            DateTime toDate;

            if (deliveryFromDate3 == "")
            {
                fromDate = DateTime.ParseExact("1981-01-01", "yyyy-MM-dd", null);
            }
            else
            {
                fromDate = DateTime.ParseExact(deliveryFromDate3, "yyyy-MM-dd", null);
            }

            if (deliveryToDate3 == "")
            {
                toDate = DateTime.ParseExact("2100-12-31", "yyyy-MM-dd", null);
            }
            else
            {
                toDate = DateTime.ParseExact(deliveryToDate3, "yyyy-MM-dd", null);
            }

            if (productID3 != null && productID3 != 0)
            {
                if (transportation3 != null && transportation3 != 0)
                {
                    res.Bill = dataContext.PtcTbl_Bills.Where(p => p.ProductID == productID3 && p.UserID == (int)Session["UserID"] && p.Type == true && p.IsDeleted == false && p.VehicleID == transportation3 && p.DateDelivery >= fromDate && p.DateDelivery <= toDate && p.PlaceDelivery.Contains(deliveryAddress3)).ToList();
                }
                else
                {
                    res.Bill = dataContext.PtcTbl_Bills.Where(p => p.ProductID == productID3 && p.UserID == (int)Session["UserID"] && p.Type == true && p.IsDeleted == false && p.DateDelivery >= fromDate && p.DateDelivery <= toDate && p.PlaceDelivery.Contains(deliveryAddress3)).ToList();
                }
            }
            else
            {
                if (transportation3 != null && transportation3 != 0)
                {
                    res.Bill = dataContext.PtcTbl_Bills.Where(p => p.UserID == (int)Session["UserID"] && p.Type == true && p.IsDeleted == false && p.VehicleID == transportation3 && p.DateDelivery >= fromDate && p.DateDelivery <= toDate && p.PlaceDelivery.Contains(deliveryAddress3)).ToList();
                }
                else
                {
                    res.Bill = dataContext.PtcTbl_Bills.Where(p => p.UserID == (int)Session["UserID"] && p.Type == true && p.IsDeleted == false && p.DateDelivery >= fromDate && p.DateDelivery <= toDate && p.PlaceDelivery.Contains(deliveryAddress3)).ToList();
                }
            }

            res.Product = dataContext.PtcTbl_Products.ToList();

            ViewBag.Totalrecords = res.Bill.Count();

            return PartialView("Import", res);
        }

        public ActionResult EditImport(DateTime deliveryDate2, int productID2, double volume2, double temp2, string deliveryAddress2, int transportation2)
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key2] == null || (bool)Session[Key2] != true)
                return RedirectToAction("Login", "Login");

            int BillID = (int)Session["BillID"];

            var dataContext = new PetroDataContext();

            PtcTbl_Bill itemEdit = dataContext.PtcTbl_Bills.FirstOrDefault(p => p.ID == BillID);

            PtcTbl_Product product = dataContext.PtcTbl_Products.FirstOrDefault(p => p.ID == productID2);
            double price = 1;

            if (product != null)
            {
                price = product.Price;
            }

            if (itemEdit != null)
            {
                itemEdit.DateDelivery = deliveryDate2;
                itemEdit.ProductID = productID2;
                itemEdit.Amount = volume2;
                itemEdit.Temperature = temp2;
                itemEdit.PlaceDelivery = deliveryAddress2;
                itemEdit.VehicleID = transportation2;
                itemEdit.Date = DateTime.Now;
                itemEdit.Price = price*volume2;
            }
            dataContext.SubmitChanges();
            return RedirectToAction("Import");
        }

        public ActionResult EditImports(int id)
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key2] == null || (bool)Session[Key2] != true)
                return RedirectToAction("Login", "Login");

            Session["BillID"] = id;
            var dataContext = new PetroDataContext();

            PtcTbl_Bill itemEdit = dataContext.PtcTbl_Bills.FirstOrDefault(p => p.ID == id);

            ViewBag.Product = dataContext.PtcTbl_Products.ToList();

            return View(itemEdit);
        }

        //------------------------------------CheckInOrder------------------------------------------------------------------------

        public ActionResult CheckIn()
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key3] == null || (bool)Session[Key3] != true)
                return RedirectToAction("Login", "Login");

            var dataContext = new PetroDataContext();
            BillAndProductAndUser itemResult = new BillAndProductAndUser();
            itemResult.Bill = dataContext.PtcTbl_Bills.Where(p => p.Type == false && p.IsDeleted == false).OrderByDescending(p => p.ID).ToList();
            itemResult.Product = dataContext.PtcTbl_Products.ToList();
            itemResult.User = dataContext.PtcTbl_Users.ToList();

            ViewBag.Totalrecords = itemResult.Bill.Count();


            if (Session["Op"] == null)
            {
                ViewBag.RecordsPerPage = 5;
            }
            else
            {
                ViewBag.RecordsPerPage = (int)Session["Op"];
            }

            return View(itemResult);
        }

        //------------------------------------CheckInMutuals------------------------------------------------------------------------

        public ActionResult ApproveOrder()
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key3] == null || (bool)Session[Key3] != true)
                return RedirectToAction("Login", "Login");

            List<int> lstId = (List<int>)Session["ListItem"];
            List<int> badID = new List<int>();

            if (lstId != null && lstId.Count > 0)
            {
                PetroDataContext dataContext = new PetroDataContext();

                PtcTbl_Bill billResult;
                foreach (int id in lstId)
                {
                    billResult = dataContext.PtcTbl_Bills.FirstOrDefault(p => p.ID == id);
                    List<PtcTbl_Tank> lstTank = dataContext.PtcTbl_Tanks.Where(p => p.ProductID == billResult.ProductID && p.IsDeleted == false).OrderByDescending(p => p.Amount).ToList();
                    var isGood = false;

                    foreach (var i in lstTank)
                    {
                        if (ValidateOrder(billResult, i) == true && billResult.Status != 1)
                        {
                            isGood = true;
                            billResult.TankID = i.ID;
                            billResult.Status = 1;
                            i.Amount = i.Amount - billResult.Amount;
                            break;
                        }
                    }

                    if (isGood == false)
                    {
                        badID.Add(id);
                    }
                }

                dataContext.SubmitChanges();
            }

            Session["succ"] = lstId.Count() - badID.Count();
            Session["fail"] = badID.Count();

            lstId = new List<int>();
            Session["ListItem"] = lstId;

            Session["BadIDList"] = badID;
            badID = new List<int>();

            return RedirectToAction("CheckIn");
        }

        public ActionResult ApproveImport()
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key3] == null || (bool)Session[Key3] != true)
                return RedirectToAction("Login", "Login");

            List<int> lstId = (List<int>)Session["ListItem"];
            List<int> badID = new List<int>();

            if (lstId != null && lstId.Count > 0)
            {
                PetroDataContext dataContext = new PetroDataContext();

                PtcTbl_Bill billResult;
                foreach (int id in lstId)
                {
                    billResult = dataContext.PtcTbl_Bills.FirstOrDefault(p => p.ID == id);
                    List<PtcTbl_Tank> lstTank = dataContext.PtcTbl_Tanks.Where(p => p.ProductID == billResult.ProductID && p.IsDeleted == false).OrderBy(p => p.Amount).ToList();
                    var isGood = false;

                    foreach (var i in lstTank)
                    {
                        if (ValidateImport(billResult, i) == true && billResult.Status != 1)
                        {
                            isGood = true;
                            billResult.TankID = i.ID;
                            billResult.Status = 1;
                            i.Amount = i.Amount + billResult.Amount;
                            break;
                        }
                    }

                    if (isGood == false)
                    {
                        badID.Add(id);
                    }
                    //billResult.Status = 1;
                }

                Session["succ"] = lstId.Count() - badID.Count();
                Session["fail"] = badID.Count();

                lstId = new List<int>();
                Session["ListItem"] = lstId;

                Session["BadIDList"] = badID;
                badID = new List<int>();

                dataContext.SubmitChanges();
            }

            return RedirectToAction("CheckInImport");
        }

        public ActionResult UncheckOrder()
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key3] == null || (bool)Session[Key3] != true)
                return RedirectToAction("Login", "Login");

            List<int> lstId = (List<int>)Session["ListItem"];
            List<int> badID = new List<int>();

            if (lstId != null && lstId.Count > 0)
            {
                PetroDataContext dataContext = new PetroDataContext();

                PtcTbl_Bill billResult;
                foreach (int id in lstId)
                {
                    billResult = dataContext.PtcTbl_Bills.FirstOrDefault(p => p.ID == id);
                    var isGood = false;

                    if (billResult != null)
                    {
                        if (billResult.Status == 1)
                        {
                            PtcTbl_Tank tankRes = dataContext.PtcTbl_Tanks.FirstOrDefault(p => p.ID == billResult.TankID);
                            if (billResult.Type == false && tankRes != null)
                            {
                                if (ValidateUncheckAndDeny(billResult, tankRes) == true)
                                {
                                    isGood = true;
                                    tankRes.Amount = tankRes.Amount + billResult.Amount;
                                    billResult.Status = 0;
                                    billResult.TankID = null;
                                }
                            }
                        }
                        else
                        {
                            billResult.Status = 0;
                        }
                    }


                    if (isGood == false)
                    {
                        badID.Add(id);
                    }
                    Session["succ"] = lstId.Count() - badID.Count();
                    Session["fail"] = badID.Count();
                }
               

                lstId = new List<int>();
                Session["ListItem"] = lstId;

                Session["BadIDList"] = badID;
                badID = new List<int>();

                dataContext.SubmitChanges();
            }

            return RedirectToAction("CheckIn");
        }

        public string GetNumberItem()
        {
            string number = Session["succ"] != null ? Session["succ"].ToString() : "0";
            return number;
        }

        public ActionResult UncheckImport()
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key3] == null || (bool)Session[Key3] != true)
                return RedirectToAction("Login", "Login");

            List<int> lstId = (List<int>)Session["ListItem"];
            List<int> badID = new List<int>();

            if (lstId != null && lstId.Count > 0)
            {
                PetroDataContext dataContext = new PetroDataContext();

                PtcTbl_Bill billResult;
                foreach (int id in lstId)
                {
                    billResult = dataContext.PtcTbl_Bills.FirstOrDefault(p => p.ID == id);
                    var isGood = false;

                    if (billResult != null)
                    {
                        if (billResult.Status == 1)
                        {
                            PtcTbl_Tank tankRes = dataContext.PtcTbl_Tanks.FirstOrDefault(p => p.ID == billResult.TankID);
                            if (billResult.Type == true && tankRes != null)
                            {
                                if (ValidateUncheckAndDeny(billResult, tankRes) == true)
                                {
                                    isGood = true;
                                    tankRes.Amount = tankRes.Amount - billResult.Amount;
                                    billResult.Status = 0;
                                    billResult.TankID = null;
                                }
                            }
                        }
                        else
                        {
                            billResult.Status = 0;
                        }
                    }


                    if (isGood == false)
                    {
                        badID.Add(id);
                    }
                    //billResult.Status = 0;
                }

                Session["succ"] = lstId.Count() - badID.Count();
                Session["fail"] = badID.Count();

                lstId = new List<int>();
                Session["ListItem"] = lstId;

                Session["BadIDList"] = badID;
                badID = new List<int>();

                dataContext.SubmitChanges();
            }

            return RedirectToAction("CheckInImport");
        }

        public ActionResult DenyOrder()
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key3] == null || (bool)Session[Key3] != true)
                return RedirectToAction("Login", "Login");

            List<int> lstId = (List<int>)Session["ListItem"];
            List<int> badID = new List<int>();

            if (lstId != null && lstId.Count > 0)
            {
                PetroDataContext dataContext = new PetroDataContext();

                PtcTbl_Bill billResult;
                foreach (int id in lstId)
                {
                    billResult = dataContext.PtcTbl_Bills.FirstOrDefault(p => p.ID == id);
                    var isGood = false;

                    if (billResult != null)
                    {
                        if (billResult.Status == 1)
                        {
                            PtcTbl_Tank tankRes = dataContext.PtcTbl_Tanks.FirstOrDefault(p => p.ID == billResult.TankID);
                            if (billResult.Type == false && tankRes != null)
                            {
                                if (ValidateUncheckAndDeny(billResult, tankRes) == true)
                                {
                                    isGood = true;
                                    tankRes.Amount = tankRes.Amount + billResult.Amount;
                                    billResult.Status = 2;
                                    billResult.TankID = null;
                                }
                            }
                        }
                        else
                        {
                            billResult.Status = 2;
                        }
                    }


                    if (isGood == false)
                    {
                        badID.Add(id);
                    }
                    Session["succ"] = lstId.Count() - badID.Count();
                    Session["fail"] = badID.Count();//billResult.Status = 2;
                }
               

                lstId = new List<int>();
                Session["ListItem"] = lstId;

                Session["BadIDList"] = badID;
                badID = new List<int>();

                dataContext.SubmitChanges();
            }

            return RedirectToAction("CheckIn");
        }

        public ActionResult DenyImport()
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key3] == null || (bool)Session[Key3] != true)
                return RedirectToAction("Login", "Login");

            List<int> lstId = (List<int>)Session["ListItem"];
            List<int> badID = new List<int>();

            if (lstId != null && lstId.Count > 0)
            {
                PetroDataContext dataContext = new PetroDataContext();

                PtcTbl_Bill billResult;
                foreach (int id in lstId)
                {
                    billResult = dataContext.PtcTbl_Bills.FirstOrDefault(p => p.ID == id);
                    var isGood = false;

                    if (billResult != null)
                    {
                        if (billResult.Status == 1)
                        {
                            PtcTbl_Tank tankRes = dataContext.PtcTbl_Tanks.FirstOrDefault(p => p.ID == billResult.TankID);
                            if (billResult.Type == true && tankRes != null)
                            {
                                if (ValidateUncheckAndDeny(billResult, tankRes) == true)
                                {
                                    isGood = true;
                                    tankRes.Amount = tankRes.Amount - billResult.Amount;
                                    billResult.Status = 2;
                                    billResult.TankID = null;
                                }
                            }
                        }
                        else
                        {
                            billResult.Status = 2;
                        }
                    }


                    if (isGood == false)
                    {
                        badID.Add(id);
                    }
                    billResult.Status = 2;
                }

                lstId = new List<int>();
                Session["ListItem"] = lstId;

                Session["BadIDList"] = badID;
                badID = new List<int>();

                dataContext.SubmitChanges();
            }

            return RedirectToAction("CheckInImport");
        }

        public bool ValidateUncheckAndDeny(PtcTbl_Bill order, PtcTbl_Tank tank)
        {
            var dataContext = new PetroDataContext();
            var lstBill = dataContext.PtcTbl_Bills.Where(p => p.Status == 1 && p.ProductID == order.ProductID).ToList();
            var finalAmount = tank.Amount;

            if (order.Type == false && order.Status == 1)
            {
                if ((order.Amount + finalAmount) <= tank.Volume)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else if (order.Type == true && order.Status == 1)
            {
                if ((finalAmount - order.Amount) >= 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }

        public bool ValidateOrder(PtcTbl_Bill order, PtcTbl_Tank tank)
        {
            var dataContext = new PetroDataContext();
            var lstBill = dataContext.PtcTbl_Bills.Where(p=> p.Status == 1 && p.ProductID == order.ProductID).ToList();
            var finalAmount = tank.Amount;

            if (order.Amount <= finalAmount)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool ValidateImport(PtcTbl_Bill order, PtcTbl_Tank tank)
        {
            var dataContext = new PetroDataContext();
            var lstBill = dataContext.PtcTbl_Bills.Where(p => p.Status == 1 && p.ProductID == order.ProductID).ToList();
            var finalAmount = tank.Amount;

            if (order.Amount <= (tank.Volume - finalAmount))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //------------------------------------CheckInImport------------------------------------------------------------------------

        public ActionResult CheckInImport()
        {
            if (Session["checkLogin"] == null || (bool)Session["checkLogin"] != true)
                return RedirectToAction("Login", "Login");
            if (Session[Key3] == null || (bool)Session[Key3] != true)
                return RedirectToAction("Login", "Login");

            var dataContext = new PetroDataContext();
            BillAndProductAndUser itemResult = new BillAndProductAndUser();
            itemResult.Bill = dataContext.PtcTbl_Bills.Where(p => p.Type == true && p.IsDeleted == false).OrderByDescending(p => p.ID).ToList();
            itemResult.Product = dataContext.PtcTbl_Products.ToList();
            itemResult.User = dataContext.PtcTbl_Users.ToList();

            ViewBag.Totalrecords = itemResult.Bill.Count();


            if (Session["Op"] == null)
            {
                ViewBag.RecordsPerPage = 5;
            }
            else
            {
                ViewBag.RecordsPerPage = (int)Session["Op"];
            }

            return View(itemResult);
        }

        //------------------------------------MutualFuncs------------------------------------------------------------------------

        

        public string AddItemIDDelete(int id)
        {
            try
            {
                List<int> lstId = (List<int>)Session["ListItemDelete"];

                lstId.Add(id);

                Session["ListItemDelete"] = lstId;
            }
            catch
            {
                List<int> lstId = new List<int>();

                lstId.Add(id);

                Session["ListItemDelete"] = lstId;
            }

            return string.Empty;
        }

        public string RemoveItemIDDelete(int id)
        {
            try
            {
                List<int> lstId = (List<int>)Session["ListItemDelete"];

                lstId.Remove(id);

                Session["ListItemDelete"] = lstId;
            }
            catch
            {

            }

            return string.Empty;
        }

        public string AddItemID(int id)
        {
            try
            {
                List<int> lstId = (List<int>)Session["ListItem"];

                lstId.Add(id);

                Session["ListItem"] = lstId;
            }
            catch
            {
                List<int> lstId = new List<int>();

                lstId.Add(id);

                Session["ListItem"] = lstId;
            }

            return string.Empty;
        }

        public string RemoveItemID(int id)
        {
            try
            {
                List<int> lstId = (List<int>)Session["ListItem"];

                lstId.Remove(id);

                Session["ListItem"] = lstId;
            }
            catch
            {

            }

            return string.Empty;
        }

        public string UpdateCBC(int c)
        {
            int count = c;

            Session["cbc"] = count;

            return string.Empty;
        }

    }

    public class BillAndProduct
    {
        public List<PTCTTCNBK.Models.PtcTbl_Bill> Bill { get; set; }
        public List<PTCTTCNBK.Models.PtcTbl_Product> Product { get; set; }       
    }

    public class BillAndProductAndUser
    {
        public List<PTCTTCNBK.Models.PtcTbl_Bill> Bill { get; set; }
        public List<PTCTTCNBK.Models.PtcTbl_Product> Product { get; set; }
        public List<PTCTTCNBK.Models.PtcTbl_User> User { get; set; }
    }

}
